﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GGH
{
    public partial class Panel : Form
    {
        GghServices.ServiceClient Connection;
        public Panel()
        {
            InitializeComponent();
            Connection = new GghServices.ServiceClient();
        }

        #region számlakészítés
        private void számlakészítésToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cb_vendeg.DataSource = Connection.GetAllGuest().Tables[0];
            cb_vendeg.DisplayMember = "Name";
            cb_vendeg.ValueMember = "Id";
            SzamlakeszitesPanel.Visible = true;

        }
        private void cb_vendeg_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int a = int.Parse(cb_vendeg.SelectedValue.ToString());
                if (a > 0)
                {
                    var dt = Connection.GetGuestByID(a).Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        tb_veznev.Text = dt.Rows[0]["name"].ToString();

                        tb_email.Text = dt.Rows[0]["email"].ToString();
                        tb_lakcim.Text = dt.Rows[0]["address"].ToString();
                    }
                }
            }
            catch { }
        }

        #endregion

        #region UIevents

        //főmenü panel tools
        private void vezetőToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VezetoPanel.Visible = true;
            VendegSzobafPanel.Visible = false;
            BejelPanel.Visible = false;
            SzamlakeszitesPanel.Visible = false;
            SzallodaiEszkozokKimutatasaPanel.Visible = false;
            UtazasiIzodakReszereInformacioPanel.Visible = false;
        }

        private void dolgozóToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VezetoPanel.Visible = false;
            VendegSzobafPanel.Visible = false;
            BejelPanel.Visible = false;
            SzamlakeszitesPanel.Visible = false;
            SzallodaiEszkozokKimutatasaPanel.Visible = false;
            UtazasiIzodakReszereInformacioPanel.Visible = false;
        }

        private void bejelentkezésToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BejelPanel.Visible = true;
            VezetoPanel.Visible = false;
            VendegSzobafPanel.Visible = false;
            SzamlakeszitesPanel.Visible = false;
            SzallodaiEszkozokKimutatasaPanel.Visible = false;
            UtazasiIzodakReszereInformacioPanel.Visible = false;
        }

        //almenük panel tools
        private void vendégSzobafoglalásToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VendegSzobafPanel.Visible = true;

        }
        private void szállodaiEszközökKimutatásaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SzallodaiEszkozokKimutatasaPanel.Visible = true;
            cb_room.DataSource = Connection.GetAllRoomByHotelId(1).Tables[0];
            cb_room.DisplayMember = "name";
            cb_room.ValueMember = "id";
        }

        private void utazásiIrodákRészéreInformációToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VendegSzobafPanel.Visible = true;
            VendegSzobafPanel.Controls["btn_foglal"].Visible = false;
            
            //UtazasiIzodakReszereInformacioPanel.Visible = true;
        }

        //panelen belüli panel tools
        private void kimut_rb_CheckedChanged(object sender, EventArgs e)
        {
            VezetoPanel2.Enabled = rb_kimut.Checked;
        }

        private void keresk_rb_CheckedChanged(object sender, EventArgs e)
        {
            VezetoPanel3.Enabled = rb_keresk.Checked;
        }

        //btn tools
        private void btn_bejel_Click(object sender, EventArgs e)
        {
            var b = Connection.GetUseRolerbyNameAndPassword(tb_felh.Text, tb_jelsz.Text);


            switch (b)
            {
                case "Boss":
                    BejelPanel.Visible = false;
                    dolgozóToolStripMenuItem.Visible = true;
                    vezetőToolStripMenuItem.Visible = true;
                    bejelentkezésToolStripMenuItem.Visible = false;
                    kijelentkezésToolStripMenuItem.Visible = true;
                    break;
                case "Worker":
                    bejelentkezésToolStripMenuItem.Visible = false;
                    dolgozóToolStripMenuItem.Visible = true;
                    kijelentkezésToolStripMenuItem.Visible = true;
                    BejelPanel.Visible = false;
                    break;
                case "External":
                    bejelentkezésToolStripMenuItem.Visible = false;
                    dolgozóToolStripMenuItem.Visible = true;
                    vendégSzobafoglalásToolStripMenuItem.Visible = false;
                    számlakészítésToolStripMenuItem.Visible = false;
                    szállodaiEszközökKimutatásaToolStripMenuItem.Visible = false;
                    kijelentkezésToolStripMenuItem.Visible = true;
                    BejelPanel.Visible = false;
                    break;
                default:
                    
                    break;
            }
            tb_jelsz.Text = "";
        }

        private void SzallodaiEszkozokKimutatasaPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void kijelentkezésToolStripMenuItem_Click(object sender, EventArgs e)
        {


            foreach (var item in Panel.ActiveForm.Controls)
            {
                if (item is Panel) ((Panel)item).Visible = false;

            }
            BejelPanel.Visible = true;
            foreach (ToolStripMenuItem item in menuStrip1.Items)
            {
                item.Visible = false;
            }
            bejelentkezésToolStripMenuItem.Visible = true;

        }

        #endregion UIevents

        private void btn_szamla_keszites_Click(object sender, EventArgs e)
        {
            //lezárni a foglalást,
            //TODO : még nem rendben a foglalás módosítása
            Connection.CreateBill(int.Parse(cb_vendeg.SelectedValue.ToString()), int.Parse(tb_fizetendo.Text.ToString()), DateTime.Now, 1);
            SzamlakeszitesPanel.Visible = false;
        }

        private void VendegSzobafPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btn_uressz_Click(object sender, EventArgs e)
        {
            var dt = Connection.GetFreeRoom().Tables[0];
            DataView dv = dt.DefaultView;
            if (cb_szobaszam.SelectedIndex > -1) dv.RowFilter = "capacity =" + cb_szobaszam.SelectedIndex.ToString();
            szabadSzobak.DataSource = dt;
            szabadSzobak.DisplayMember = "name";
            szabadSzobak.ValueMember = "id";
            var dt1 = Connection.GetAllGuest().Tables[0];
            cb_vendeglista.DataSource = dt1;
            cb_vendeglista.ValueMember = "Name";
            cb_vendeglista.ValueMember = "id";
        }

        private void btn_foglal_Click(object sender, EventArgs e)
        {
            Connection.InsertReservation(int.Parse(szabadSzobak.SelectedValue.ToString()),
                                         "", DateTime.Now, DateTime.Now.AddDays(14), "Foglalt");
            MessageBox.Show("A Foglalást rögzítettük.");
        }
        private UserInsert insertform = new UserInsert();
        private void button1_Click(object sender, EventArgs e)
        {

            if (insertform.ShowDialog() == DialogResult.OK)
            {
                Connection.InsertGuest(insertform.GuestName, insertform.Email, insertform.Address);
                var dt1 = Connection.GetAllGuest().Tables[0];
                cb_vendeglista.DataSource = dt1;
                cb_vendeglista.DisplayMember = "Name";
                cb_vendeglista.ValueMember = "id";

            }
        }




#region room
        //szoba inzert
        private void button2_Click(object sender, EventArgs e)
        {
            Room roomform = new Room();
            if (roomform.ShowDialog() == DialogResult.OK)
            {
                Connection.InsertRoom(roomform.RoomName, 1, roomform.Capacity, roomform.Desc);
            }
            cb_room.DataSource = Connection.GetAllRoomByHotelId(1).Tables[0];
            cb_room.DisplayMember = "name";
            cb_room.ValueMember = "id";

        }


        //szoba módosítás
        private void button8_Click(object sender, EventArgs e)
        {
            var dt = Connection.GetRoomById(int.Parse(cb_room.SelectedValue.ToString())).Tables[0];
            if (dt.Rows.Count == 1)
            {
                Room roomform = new Room();
                roomform.RoomName = dt.Rows[0]["name"].ToString();
                roomform.Desc = dt.Rows[0]["Description"].ToString();
                roomform.Capacity = int.Parse(dt.Rows[0]["Capacity"].ToString());
                if (roomform.ShowDialog() == DialogResult.OK)
                {
                    Connection.ModifyRoom(roomform.RoomName, 1, roomform.Capacity, roomform.Desc, int.Parse(cb_room.SelectedValue.ToString()));
                }
                cb_room.DataSource = Connection.GetAllRoomByHotelId(1).Tables[0];
                cb_room.DisplayMember = "name";
                cb_room.ValueMember = "id";
            }
        }

        private void cb_room_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var dt = Connection.GetPremisesByRoomId(int.Parse(cb_room.SelectedValue.ToString())).Tables[0];
                if (dt.Rows.Count >0)
                {
                    cb_prem.DataSource = dt;
                    cb_prem.ValueMember = "id";
                    cb_prem.DisplayMember = "type";
                }
                else
                {
                    cb_prem.DataSource = new DataTable();
                    cb_element.DataSource = new DataTable();
                }
            }
            catch
            {
                cb_prem.DataSource = new DataTable();
                cb_element.DataSource = new DataTable();
            }
        }
#endregion
#region premise
        //helyiség insert
        private void button7_Click(object sender, EventArgs e)
        {
            premise premiseform = new premise();
            if (premiseform.ShowDialog() == DialogResult.OK)
            {
                Connection.InsertPremise(premiseform.Type,
                int.Parse(cb_room.SelectedValue.ToString()),
                premiseform.window, premiseform.balcony, premiseform.climate);
            }
            //akár listát is visszaadhat.
            var dt = Connection.GetPremisesByRoomId(int.Parse(cb_room.SelectedValue.ToString())).Tables[0];
            cb_prem.DataSource = dt;
            cb_prem.ValueMember = "id";
            cb_prem.DisplayMember = "type";

        }
        //helyiség módosítás
        private void button6_Click(object sender, EventArgs e)
        {
            var dt = Connection.GetPremiseById(int.Parse(cb_prem.SelectedValue.ToString())).Tables[0];
            if (dt.Rows.Count == 1)
            {
                premise premiseform = new premise();
                premiseform.Type = dt.Rows[0]["type"].ToString();
                premiseform.window = int.Parse(dt.Rows[0]["window"].ToString());
                premiseform.balcony = int.Parse(dt.Rows[0]["balcony"].ToString());
                premiseform.climate = int.Parse(dt.Rows[0]["climate"].ToString());
                if (premiseform.ShowDialog() == DialogResult.OK)
                {
                    Connection.ModifyPremise(premiseform.Type, int.Parse(dt.Rows[0]["room_id"].ToString()), premiseform.window, premiseform.balcony, premiseform.climate, int.Parse(dt.Rows[0]["id"].ToString()));
                    cb_prem.DataSource = Connection.GetPremisesByRoomId(int.Parse(cb_room.SelectedValue.ToString())).Tables[0]; ;
                    cb_prem.ValueMember = "id";
                    cb_prem.DisplayMember = "type";

                }
            }
        }
        private void cb_prem_SelectedIndexChanged(object sender, EventArgs e)
        {
            //beállítani hogyha dt.rows.count == 0 akkor enable=false legyen.
            try
            {
               var dt = Connection.GetElementByPremiseId(int.Parse(cb_prem.SelectedValue.ToString())).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    cb_element.DataSource = dt;
                    cb_element.ValueMember = "id";
                    cb_element.DisplayMember = "type";
                }
                else
                {

                    cb_element.DataSource = new DataTable();
                    
                }
            }
            catch {
                cb_element.DataSource = new DataTable();
            }
        }
#endregion premise
#region element

        //element insert
        private void button5_Click(object sender, EventArgs e)
        {
            Element elementform = new Element();
            if (elementform.ShowDialog() == DialogResult.OK)
            {
                Connection.InsertElement(int.Parse(cb_prem.SelectedValue.ToString()), elementform.type,int.Parse( elementform.piece), elementform.status, elementform.subtype, elementform.Desc);
                cb_element.DataSource = Connection.GetElementByPremiseId(int.Parse(cb_prem.SelectedValue.ToString())).Tables[0]; ;
                cb_element.ValueMember = "id";
                cb_element.DisplayMember = "type";

            }
        }

        //element modify
        private void button3_Click(object sender, EventArgs e)
        {
            var dt = Connection.GetElementByID(int.Parse(cb_element.SelectedValue.ToString())).Tables[0];
            if (dt.Rows.Count == 1)
            {
                Element elementform = new Element(){type  = dt.Rows[0]["type"].ToString(),
                                                    piece = dt.Rows[0]["piece"].ToString(),
                                                    status = dt.Rows[0]["status"].ToString(),
                                                    subtype = dt.Rows[0]["subtype"].ToString(),
                                                    Desc = dt.Rows[0]["Description"].ToString()};
                
                if( elementform.ShowDialog()== DialogResult.OK)
                {
                    Connection.ModifyElement(int.Parse(dt.Rows[0]["premise_id"].ToString()),
                                             elementform.type,
                                             0,
                                             elementform.status,
                                             elementform.subtype,
                                             elementform.Desc,
                                             int.Parse(dt.Rows[0]["id"].ToString()));
                    cb_element.DataSource = Connection.GetElementByPremiseId(int.Parse(cb_prem.SelectedValue.ToString())).Tables[0]; ;
                    cb_element.ValueMember = "id";
                    cb_element.DisplayMember = "type";

                }
            }
        }
        private void cb_element_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
#endregion element
        #region boss function
        private void btn_vegrehajt_Click(object sender, EventArgs e)
        {
            switch (kimu_typ_cb.SelectedIndex)
	{
                case 0:
                    var ds = Connection.GetBillByDateBetween(kezdet_dt.Value, veg_dt.Value).Tables[0];
                    GridView viewForm1 = new GridView() { Title = "egyes kimutatás", DataSource = ds };
                    viewForm1.Show();
                    break;
                case 1:
                    var ds1 = Connection.GetUsedRoom().Tables[0];
                    GridView viewForm2 = new GridView() { Title = "kettes íkimutatás", DataSource = ds1 };
                    viewForm2.Show();
                    
                    break;
                default:
                    break;
	}
            
            
        }
        #endregion bossfunction
    }
}
